<template>
	<view>
		<view class="container690 cont">
			<view class="cont1">
				<scroll-view :scroll-into-view="scrollview" class="scroll-view1" :scroll-y="true" enable-flex="true">
					<view class="left">
						<view class="add1" v-for="(item,index) in list" :id="item.letter" :key="index">
							<view class="hot" v-if="item.letter=='#'">
								<text class="title">热门城市</text>
								<view class="cityWrap">
									<view @click="changeCity(item1)" class="city"
										v-for="(item1,index1) in item.citylist" :key="index1">
										{{item1.name}}
									</view>
								</view>
							</view>
							<view v-else class="noHot">
								<text class="title">{{item.letter}}</text>
								<view @click="changeCity(item1)" class="city" v-for="(item1,index1) in item.citylist"
									:key="index1">
									{{item1.name}}
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<scroll-view class="scroll-view2" v-if="name==''" :scroll-y="true" enable-flex="true">
			<view class="right">
				<text @click="changeLetter(item)" v-for="(item,index) in letters" :key="index">{{item}}</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import address from "./address-letter.js"
	// import pinyin from "pinyin";
	export default {
		props: {
			letter: {
				type: String,
				default: 'A'
			},
			name: {
				type: String,
				default: ''
			},
		},
		watch: {
			letter: function() {
				this.scrollview = this.letter
			},
			name(data) {
				clearTimeout(this.timer)
				if(data){
				  this.timer = setTimeout(() => {
				  	address.map((item) => {
				  		for (let it of item.citylist) {
				  			if (it.name.indexOf(data) >= 0) {
				  				this.list = [{
				  						letter: '',
				  						citylist: [
				  							it
				  						]
				  				}];
				  				break;
				  			}
				  		}
				  	}, 1000)
				  })	
				}else{
					this.list=address;
				}
			}
		},
		data() {
			return {
				cityName: '重庆',
				scrollview: this.letter,
				list: address,
				letters: ["#"],
				timer: null
			}
		},
		created() {
			for (var i = 0; i < 26; i++) {
				this.letters.push(String.fromCharCode((65 + i)))
			}
			console.log(this.letters)
		},
		methods: {
			changeLetter(id) {
				this.scrollview = id
			},
			changeCity(obj) {
				this.$emit('confirm', obj)
			}
		}
	}
</script>

<style lang="scss">
	.container690 {
		width: 644rpx;
		margin: 0 auto;
	}

	.cont {
		height: 100vh;
		overflow: hidden;

		.cont1 {
			display: flex;
			justify-content: space-between;
		}

		.scroll-view1 {
			width: 650rpx;
			height: 100vh;
			display: flex;
			justify-content: space-between;

			.left {
				width: 100%;

				.add1 {
					margin-top: 30rpx;
				}

				.noHot {
					.title {
						display: block;
						color: #000000;
						font-size: 32rpx;
					}

					.city {
						width: 100%;
						height: 80rpx;
						line-height: 80rpx;
						font-size: 28rpx;
						border-bottom: 1px solid #F2F2F2;
						color: #494949;
					}
				}

				.hot {
					.title {
						display: block;
						color: #000000;
						font-size: 30rpx;
					}

					.cityWrap {
						margin-top: 32rpx;
						overflow: hidden;

						.city {
							margin: 0 19rpx 20rpx 0;
							float: left;
							width: 202rpx;
							height: 66rpx;
							text-align: center;
							background: #F5F5F5;
							border-radius: 10rpx;
							line-height: 66rpx;
							font-size: 30rpx;
						}

						.city:nth-child(3n) {
							margin-right: 0;
						}
					}
				}
			}
		}
	}

	.scroll-view2 {
		position: fixed;
		right: 10rpx;
		top: 438rpx;
		width: 40rpx;
		height: 800rpx;
		padding-bottom: 30rpx;
		text-align: center;

		.right {
			width: 100%;

			text {
				text-align: center;
				display: block;
				color: #666666;
				font-size: 18rpx;
				padding: 3rpx 0;
			}
		}
	}
</style>
